<?php
function fw_util_fetchPostValue($key = '')
{
	return $_POST[$key];
}

function fw_util_fetchGetValue($key = '')
{
	return $_GET[$key];
}

function fw_util_makeSessionKey()
{
	$s_userAgent = $_SERVER['HTTP_USER_AGENT'];
	$s_timestamp = date("Y-m-d H:i:s:ss");
	$s_first_key = substr($s_timestamp, rand(2,9), rand(2,9));
	$s_second_key = substr($s_userAgent, rand(2,9), rand(2,9));
	$s_third_key = substr($s_timestamp , rand(1,8), rand(1,8));
	$s_fourth_key = substr($s_userAgent , rand(1,8), rand(1,8));
	$s_concat_Key = $s_first_key.$s_second_key.$s_third_key.$s_fourth_key;
	$s_sessionKey = sha1(hash('md5',$s_concat_Key));
	return $s_sessionKey;
}

function fw_util_encrypt($s_rawString, $i_subCount = 20)
{
	$s_salt = 'saltedString';
	$s_rawEncryption = hash('md5', $s_salt . $s_rawString . $s_salt);
	$s_finalEncryption = substr($s_rawEncryption, 0, $i_subCount);
	return $s_finalEncryption;
}

function fw_util_outputJson($arr_inputArr)
{
	require C_FW_fwPackage.'utilities/json/JSON.php';
	header('Cache-Control: no-cache, must-revalidate');
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Content-type: application/json');
	die(json_encode($arr_inputArr));
}

function fw_util_decodeJson($arr_inputJson)
{
	require C_FW_fwPackage.'utilities/json/JSON.php';
	return json_decode($arr_inputJson);
}
//new functions
function fw_util_getAppController($s_controllerAlias)
{
	$s_controllerClass = '';
	include C_FW_customPackage.'configs/applications.php';
	if(isset($arr_applications) && !empty($arr_applications))
	{
		$i_arrCount = count($arr_applications);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_app = $arr_applications[$i];
			$s_arrKey = array_search($s_controllerAlias, $arr_app, true);
			
			if($s_arrKey !== false)
			{
				$s_controllerClass = $arr_app['controller_class'];
			}
			
		}		
	}
	return $s_controllerClass;
}

function fw_util_findArrValue($arr_hayStack, $s_needle)
{
	$s_arrValue = '';
	
	if(isset($arr_hayStack) && !empty($arr_hayStack))
	{
		$i_arrCount = count($arr_hayStack);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_inHayStack = $arr_hayStack[$i];
			$s_arrFlag = array_search($s_needle, $arr_inHayStack, true);
				
			if($s_arrFlag !== false)
			{
				$s_arrValue = $arr_inHayStack;
			}
				
		}
	}
	return $s_arrValue;
}

function fw_util_getAppIdent($s_controllerAlias)
{
	$s_appIdent = '';
	include C_FW_customPackage.'configs/applications.php';
	if(isset($arr_applications) && !empty($arr_applications))
	{
		$i_arrCount = count($arr_applications);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_app = $arr_applications[$i];
			$s_arrKey = array_search($s_controllerAlias, $arr_app, true);
				
			if($s_arrKey !== false)
			{
				$s_appIdent = $arr_app['app_ident'];
			}
				
		}
	}
	return $s_appIdent;
}


function fw_util_getScripts()
{
	$s_scriptsInc = '';
	include C_FW_customPackage.'configs/applications.php';
	if(isset($arr_applications) && !empty($arr_applications))
	{
		$i_arrCount = count($arr_applications);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_app = $arr_applications[$i];
			if(isset(Claycode_Router::getInstance()->s_currAppIdent)){
				$s_arrKey = array_search(Claycode_Router::getInstance()->s_currAppIdent, $arr_app, true);
				
				if($s_arrKey !== false)
				{
					$arr_cssFiles = $arr_app['css_style_sheets'];
					$arr_jsFiles = $arr_app['js_files'];
					
					if(!empty($arr_cssFiles))
					{
						$i_cssFilesCount = count($arr_cssFiles);
						for($x=0;$x<$i_cssFilesCount;$x++)
						{
							$s_scriptsInc .= '<link rel="stylesheet" href="'.$arr_cssFiles[$x].'" type="text/css" media="screen" />';
						}
					}
					
					if(!empty($arr_jsFiles))
					{
						$i_jsFilesCount = count($arr_jsFiles);
						for($y=0;$y<$i_jsFilesCount;$y++)
						{
							$s_scriptsInc .= '<script type="text/javascript" src="'.$arr_jsFiles[$y].'" ></script>';
						}
					}
				}
			}
		
		}
	}
	return $s_scriptsInc;
}

function fw_util_getjsLoadFunction()
{
	$s_jsLoadFunction = '';
	include C_FW_customPackage.'configs/applications.php';
	if(isset($arr_applications) && !empty($arr_applications))
	{
		$i_arrCount = count($arr_applications);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_app = $arr_applications[$i];
			if(isset(Claycode_Router::getInstance()->s_currAppIdent)){
				$s_arrKey = array_search(Claycode_Router::getInstance()->s_currAppIdent, $arr_app, true);

				if($s_arrKey !== false)
				{
					$s_jsLoadFunction = $arr_app['js_load_function'];
						
				}
			}

		}
	}
	return $s_jsLoadFunction;
}

function fw_util_getAppDisplayName()
{
	$s_jsLoadFunction = '';
	include C_FW_customPackage.'configs/applications.php';
	if(isset($arr_applications) && !empty($arr_applications))
	{
		$i_arrCount = count($arr_applications);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_app = $arr_applications[$i];
			if(isset(Claycode_Router::getInstance()->s_currAppIdent)){
				$s_arrKey = array_search(Claycode_Router::getInstance()->s_currAppIdent, $arr_app, true);

				if($s_arrKey !== false)
				{
					$s_jsLoadFunction = $arr_app['app_display_name'];

				}
			}

		}
	}
	return $s_jsLoadFunction;
}

function fw_util_getAppConfArr()
{
	$arr_appConfArr = array();
	include C_FW_customPackage.'configs/applications.php';
	if(isset($arr_applications) && !empty($arr_applications))
	{
		$i_arrCount = count($arr_applications);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_app = $arr_applications[$i];
			if(isset(Claycode_Router::getInstance()->s_currAppIdent)){
				$s_arrKey = array_search(Claycode_Router::getInstance()->s_currAppIdent, $arr_app, true);
			
				if($s_arrKey !== false)
				{
					$arr_appConfArr = $arr_app;
				}
			}
	
		}
	}
	
	return $arr_appConfArr;
}

function fw_util_arrayPush($arr_base, $o_input){
	if(is_array($o_input) == true){
		$arr_base = array_merge($arr_base, $o_input);
	}else{
		array_push($arr_base, $o_input);
	}
	return $arr_base;
}

?>